package com.maila.xiaoaoschool.activity;

import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.adapter.CommentAdapter;
import com.maila.xiaoaoschool.api.FindApi;
import com.maila.xiaoaoschool.api.ShopApi;
import com.maila.xiaoaoschool.entity.ShopClasses;
import com.maila.xiaoaoschool.entity.ShopComment;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.maila.xiaoaoschool.util.Constants;
import com.maila.xiaoaoschool.widget.NoScrollListView;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by mark on 2017/9/27.
 */

public class FoodDetailActivity extends AppCompatActivity {
    @BindView(R.id.iv_food)
    ImageView ivFood;
    @BindView(R.id.btn_cancel)
    AutoRelativeLayout btnCancel;
    @BindView(R.id.iv_collect)
    ImageView ivCollect;
    @BindView(R.id.btn_collect)
    AutoRelativeLayout btnCollect;
    @BindView(R.id.tv_food_name)
    TextView tvFoodName;
    @BindView(R.id.tv_food_xiaoliang)
    TextView tvFoodXiaoliang;
    @BindView(R.id.tv_fee)
    TextView tvFee;
    @BindView(R.id.tv_yuan)
    TextView tvYuan;
    @BindView(R.id.btn_add_to_car)
    TextView btnAddToCar;
    @BindView(R.id.tv_comment_count)
    TextView tvCommentCount;
    @BindView(R.id.lv_comment)
    NoScrollListView lvComment;
    int iscollect = 0;
    CommentAdapter commentAdapter;
    int goodID ;
    ShopClasses.DataBean.GoodBean goodBean;
    private int page = 1;
    private int size = 8;
    Gson gson = new Gson();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_details);
        ButterKnife.bind(this);
        commentAdapter = new CommentAdapter(this);
        goodBean = (ShopClasses.DataBean.GoodBean) getIntent().getSerializableExtra("data");

        goodID = goodBean.getGoodsid();
        lvComment.setAdapter(commentAdapter);
        initdata();
        CheckCollection();
    }

    private void CheckCollection() {
        new ShopApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    if(jsonObject.optInt("result")==1){
                        if(jsonObject.optInt("data")==1){
                            iscollect = 1;
                            ivCollect.setImageResource(R.mipmap.shoucanghong);
                        } else {
                            iscollect = 0;
                            ivCollect.setImageResource(R.mipmap.weishoucang);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).checkcollection(goodID);
    }

    private void initdata() {
//加载商品信息
        if(goodBean!=null){
            Glide.with(this).load(Uri.parse(Constants.URL_ROOT_XIAOAO+goodBean.getImg())).into(ivFood);
            tvFoodName.setText(goodBean.getGoodsname());
            tvFoodXiaoliang.setText("销量："+goodBean.getSellcount()+"份");
            if(goodBean.getIs_cx()==1){
                tvYuan.setText("￥"+goodBean.getCost());
                tvYuan.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
                tvFee.setText("￥"+goodBean.getCxcost());
            }else {
                tvFee.setText("￥"+goodBean.getCost());
                tvYuan.setVisibility(View.GONE);
            }

        }

        new ShopApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                ShopComment shopComment = gson.fromJson(result, ShopComment.class);
                if (shopComment.getResult() == 1) {
                    if (page == 1) {
                        commentAdapter.setMdata(shopComment.getData());
                    } else {
                        commentAdapter.addMdata(shopComment.getData());
                    }
                    ++page;
                } else {

                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getGoodsCpmments(goodID, page, size);
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestViewFoucus(ivFood);
    }

    private void requestViewFoucus(View view) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
    }

    @OnClick({R.id.btn_cancel, R.id.btn_collect, R.id.btn_add_to_car})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_collect:
                if (iscollect == 1) {
                    iscollect = 0;
                    ivCollect.setImageResource(R.mipmap.weishoucang);
                } else {
                    iscollect = 1;
                    ivCollect.setImageResource(R.mipmap.shoucanghong);
                }
                break;
            case R.id.btn_add_to_car:
                break;
        }
    }
}
